﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using OpalisConsole.EndUserService;
using OpalisConsole.Views.Administration.Child_Windows;
using System.Collections.ObjectModel;

namespace OpalisConsole.Views.Administration
{
    public partial class Administration : Page
    {
        public ObservableCollection<AuthorizationRequest> authList;
        public Administration()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (!((bool)App.Current.Resources["FreshStartAdmin"] || (bool)App.Current.Resources["FreshStartApprover"]))
            {
                this.NavigationService.Navigate(new System.Uri("/Home", System.UriKind.Relative));
            }
            else
            {
                boxFilterType.Items.Add("Machine Name");
                boxFilterType.Items.Add("User Name");
                boxFilterType.Items.Add("Status");
                boxFilterType.Items.Add("AD Site");

                boxFilterType.SelectedIndex = 0;

                authList = new ObservableCollection<AuthorizationRequest>();

                gridAuthorization.ItemsSource = authList;

                busyIndicatorUserManagement.IsBusy = true;
                busyIndicatorUserManagement.BusyContent = "Loading Information";

                EndUserServiceSoapClient proxy = new EndUserServiceSoapClient();
                proxy.getPendingRequestsCompleted += new EventHandler<getPendingRequestsCompletedEventArgs>(proxy_getPendingRequestsCompleted);
                proxy.getPendingRequestsAsync();
            }
        }
        private void boxFilter_TextChanged(object sender, TextChangedEventArgs e)
        {
            ObservableCollection<AuthorizationRequest> approvalList = (ObservableCollection<AuthorizationRequest>)App.Current.Resources["ApprovalList"];
            if (boxFilter.Text != String.Empty)
            {
                ObservableCollection<AuthorizationRequest> FilterApprovalList = new ObservableCollection<AuthorizationRequest>();
                foreach (AuthorizationRequest request in approvalList)
                {
                    switch ((String)boxFilterType.SelectedItem)
                    {
                        case "Machine Name":
                            if (request.MachineName.ToUpper().Contains(boxFilter.Text.ToUpper()))
                            {
                                FilterApprovalList.Add(request);
                            }
                            break;
                        case "User Name":
                            if (request.UserName.ToUpper().Contains(boxFilter.Text.ToUpper()))
                            {
                                FilterApprovalList.Add(request);
                            }
                            break;
                        case "Status":
                            if (request.Status.ToUpper().Contains(boxFilter.Text.ToUpper()))
                            {
                                FilterApprovalList.Add(request);
                            }
                            break;
                        case "AD Site":
                            if (request.ADSite.ToUpper().Contains(boxFilter.Text.ToUpper()))
                            {
                                FilterApprovalList.Add(request);
                            }
                            break;
                    }
                }

                gridAuthorization.ItemsSource = FilterApprovalList;

            }
            else
            {
                gridAuthorization.ItemsSource = approvalList;
            }
        }
        
        void proxy_getPendingRequestsCompleted(object sender, getPendingRequestsCompletedEventArgs e)
        {
            busyIndicatorUserManagement.IsBusy = false;
            if (e.Error != null)
            {
                throw new Exception();
            }
            else
            {
                List<AuthorizationRequest> tList = new List<AuthorizationRequest>();
                foreach (AuthorizationRequest req in e.Result)
                {
                    AuthorizationRequest tempReq = req;
                    tempReq.LastUpdatedTime = tempReq.LastUpdatedTime.ToLocalTime();
                    tempReq.EnteredTime = tempReq.EnteredTime.ToLocalTime();

                    tList.Add(tempReq);
                }
                AuthorizationRequestComparer arc = new AuthorizationRequestComparer();
                tList.Sort(arc);

                ObservableCollection<AuthorizationRequest> fList = new ObservableCollection<AuthorizationRequest>();
                foreach (AuthorizationRequest req in tList)
                {
                    authList.Add(req);
                }

                if (App.Current.Resources.Contains("ApprovalList"))
                {
                    App.Current.Resources.Remove("ApprovalList");
                    App.Current.Resources.Add("ApprovalList", authList);
                }
                else
                {
                    App.Current.Resources.Add("ApprovalList", authList);
                }

                
            }
        }

        private void gridAuthorization_GotFocus(object sender, RoutedEventArgs e)
        {
            AuthorizationRequest request = (AuthorizationRequest)gridAuthorization.SelectedItem;
            AuthorizationDetailsWindow AuthDeatilsWindow = new AuthorizationDetailsWindow(request);
            AuthDeatilsWindow.Closed += new EventHandler(AuthDeatilsWindow_Closed);
            AuthDeatilsWindow.Show();
        }

        void AuthDeatilsWindow_Closed(object sender, EventArgs e)
        {
            AuthorizationDetailsWindow AuthDeatilsWindow = (AuthorizationDetailsWindow)sender;
            AuthorizationRequest request = AuthDeatilsWindow.req;
            EndUserServiceSoapClient proxy = new EndUserServiceSoapClient();

            String requester = Application.Current.Resources["UserID"].ToString();
            String domain = Application.Current.Resources["UserDomain"].ToString();

            

            /*
            busyIndicatorUserManagement.IsBusy = true;
            busyIndicatorUserManagement.BusyContent = "Loading Information";
            */
            
            foreach (AuthorizationRequest req in authList)
            {
                if (req.MachineName.Equals(request.MachineName) && req.UserName.Equals(request.UserName))
                {
                    switch(AuthDeatilsWindow.req.Status)
                    {
                        case "Approved":
                            proxy.addComputerToApproveComptuersAsync(request.MachineName);
                            proxy.closePendingApprovalRequestAsync(request.MachineName, request.UserName, "Approved", requester, domain);
                            req.Status = "Approved";
                            break;
                        case "Pending":
                            proxy.closePendingApprovalRequestAsync(request.MachineName, request.UserName, "Pending", requester, domain);
                            req.Status = "Pending";
                            break;
                        default:
                            break;
                    }
                }
            }
            /*
            EndUserServiceSoapClient proxy = new EndUserServiceSoapClient();
            proxy.getPendingRequestsCompleted += new EventHandler<getPendingRequestsCompletedEventArgs>(proxy_getPendingRequestsCompleted_later);
            proxy.getPendingRequestsAsync();*/
        }
        /*
        void proxy_getPendingRequestsCompleted_later(object sender, getPendingRequestsCompletedEventArgs e)
        {
            busyIndicatorUserManagement.IsBusy = false;
            if (e.Error != null)
            {
                throw new Exception();
            }
            else
            {
                authList.Clear();

                List<AuthorizationRequest> tList = new List<AuthorizationRequest>();
                foreach (AuthorizationRequest req in e.Result)
                {
                    tList.Add(req);
                }
                AuthorizationRequestComparer arc = new AuthorizationRequestComparer();
                tList.Sort(arc);

                ObservableCollection<AuthorizationRequest> fList = new ObservableCollection<AuthorizationRequest>();
                foreach (AuthorizationRequest req in tList)
                {
                    authList.Add(req);
                }

                if (App.Current.Resources.Contains("ApprovalList"))
                {
                    App.Current.Resources.Remove("ApprovalList");
                    App.Current.Resources.Add("ApprovalList", authList);
                }
                else
                {
                    App.Current.Resources.Add("ApprovalList", authList);
                }

                
            }
        }
         * */
    }
    public class AuthorizationRequestComparer : IComparer<AuthorizationRequest>
    {
        public int Compare(AuthorizationRequest x, AuthorizationRequest y)
        {
            if (x == null)
            {
                if (y == null)
                {
                    return 0;
                }
                else
                {
                    return -1;
                }
            }
            else
            {
                if (x.Status == null)
                {
                    if (y.Status == null)
                    {
                        return 0;
                    }
                    else
                    {
                        return -1;
                    }
                }
                else
                {
                    if (y.Status == null)
                    {
                        return 1;
                    }
                    else
                    {
                        return (x.Status.CompareTo(y.Status));
                    }
                }
            }
        }
    }
}
